package com.htc.callcare.viewcontroller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.htc.callcare.R;
import com.htc.callcare.activities.SettingActivity;
import com.htc.callcare.database.Database;
import com.htc.callcare.infrastructure.globalVar;
import com.htc.callcare.model.NoteModel;

import android.content.Context;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.media.MediaRecorder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

public class Popup_Call extends LinearLayout {

	Button mBtnRecord;
	Button mBtnNote;
	EditText edtNotePad;
	Context context;
	String phoneNumber;
	public static Popup_Call instance;
	MediaRecorder recorder;
	static boolean isrecord;
	String outputFormat;
	int type;
	NoteModel noteModel;
	String path = globalVar.recordDir;
	public Popup_Call(Context context) {
		super(context);
		this.context = context;
		// TODO Auto-generated constructor stub
		LayoutInflater.from(context).inflate(R.layout.popup_call, this, true);
		mBtnRecord = (Button) findViewById(R.id.btnPopupRecord);
		mBtnNote = (Button) findViewById(R.id.btnPopupNote);
		edtNotePad = (EditText) findViewById(R.id.notepad);
		isrecord = false;
		initListener();
		noteModel = new NoteModel();
	}

	private void initListener() {
		// TODO Auto-generated method stub
		mBtnRecord.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isrecord) {
					startrecord();
				} else {
					endrecord();
				}
			}
		});

		mBtnNote.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(edtNotePad.isShown()){
					edtNotePad.setVisibility(GONE);
				}else{
					edtNotePad.setVisibility(VISIBLE);
				}
			}
		});
	}

	public static Popup_Call getinstance(Context context) {
		if (instance == null) {
			instance = new Popup_Call(context);
		}
		return instance;
	}

	// get phone state in offHook state
	public void setPhoneNumber(String phoneNum) {
		this.phoneNumber = phoneNum;
	}

	public void setType(int type) {
		this.type = type;
	}

	public void setOutputFormat(String format) {
		this.outputFormat = format;
	}

	public void startrecord() {
		
		String extension = ".amr";//.mp4, gpp
		String name = "_";
		globalVar.makeDir();
		try {
			mBtnNote.setEnabled(true);
			recorder = new MediaRecorder();
			isrecord = true;
			// recordbtn.setText("Recording");
			mBtnRecord.setText("Recording");
			
			recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);
			
			/*Toast.makeText(getContext(), outputFormat, Toast.LENGTH_SHORT).show();
			if (SettingActivity.mp4.isChecked()) {
				recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
				extension = ".mp4";
			} else if (SettingActivity.gpp.isChecked()) {
				recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
				extension = ".3gp";
			} else {
				recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
				extension = ".amr";
			}*/
			recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
			//recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
			//recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			Date date = new Date();
			DateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
			int minute = Calendar.getInstance().get(Calendar.MINUTE);
			name = "_" + Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + "h"
					+ (((minute<10)? "0" :"")+minute) + "_"
					+ datef.format(date) + "_" + phoneNumber + ".amr";
			// type 1 : incoming
			// type 2 : outcoming
			if (type == 1) {
				name = globalVar.incoming + name;
			} else {
				name = globalVar.outgoing + name;
			}
			noteModel.setFileUri(name);
			path += name;
			recorder.setOutputFile(path);
			recorder.prepare();
			recorder.start();
		} catch (Exception e) {
			isrecord = false;
			// recordbtn.setText("Record");
			mBtnRecord.setText("Record");
			Toast.makeText(context,
					"Directory is not availble " + e.getMessage(),
					Toast.LENGTH_SHORT).show();

			e.printStackTrace();
		}
	}

	public void endrecord() {
		try {
			if (isrecord) {
				recorder.stop();
				recorder.reset(); // You can reuse the object by going back to
				recorder.release(); // Now the object cannot be reused
				noteModel.setNoteContent(edtNotePad.getText().toString().trim());
				
				Database database = new Database(context);
				database.addNote(noteModel);
				Log.v("thuutai", "Added");
			}
		} catch (Exception e) {

		}
		isrecord = false;
		// recordbtn.setText("Record");
		mBtnRecord.setText("Record");
	}

}
